﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	SelezLinea
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Globalization;

namespace Map_Fvg
{
    public partial class SelezLinea : Form
    {

        public SelezLinea()
        {
            InitializeComponent();
            if (ViewQ.inserimento_modo == 1)
            {
                label1.Text = "inserimento della linea  n°" ;
                button2.Text = "inserisci";
                label6.Visible = false;
                textBox3.Visible = false;
                button3.Visible = false;
            }

            if (ViewQ.inserimento_modo == 2)            //      ampliamento ultima linea
            {
               label6.Visible = false;
                textBox3.Visible = false;
                button3.Visible = false;
            }
//            button4.Visible = false;        //********************
  //          button5.Visible = false;        //********************
        }

        private void SelezLinea_Load(object sender, EventArgs e)
        {
            if (ViewQ.error_flag == 1)
            {
                this.DialogResult = DialogResult.OK;
                return;
            }

             int isel = 0;
             int Vertices = 0;
             if (ViewQ.inserimento_modo == 1)
             {
                 isel = ViewQ.nShapes;
                 Vertices = ViewQ.newVertices;
             }
             else
             {
                isel = ViewQ.selected_linea;
                Vertices = ViewQ.vectShapes[isel].nVertices;
             }
            textBox1.Text = isel.ToString();
            textBox2.Text = Vertices.ToString();
            if (ViewQ.selected_point >= 0)
            {
                textBox3.Text = ViewQ.selected_point.ToString();
                textBox3.Visible = true;
                label6.Visible = true;
            }
            else
            {
                label6.Visible = false;
            }

            if (ViewQ.inserimento_modo > 0)
            {
                vectShapesQuota(isel);
                textBox4.Text = ViewQ.vectShapes[isel].quota;
                button4.Visible = false;
                vectShapesLunghezza(isel, Vertices);
                textBox5.Text = ViewQ.vectShapes[isel].lunghezza;
                button5.Visible = false;
            }
            else
            {
                textBox4.Text = ViewQ.vectShapes[isel].quota;
                button4.Visible = true;
                textBox5.Text = ViewQ.vectShapes[isel].lunghezza;
                button5.Visible = true;
            }
            button4.Visible = false;        //********************
            button5.Visible = false;        //********************

            textBox6.Text = ViewQ.vectShapes[isel].edizione;
            if (textBox6.Text.Contains("1")) textBox6.Text = "2";
            if (ViewQ.inserimento_modo == 1) textBox6.Text = "3";

            int pntSimb = ViewQ.vectShapes[isel].pntSimb;

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from classi";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            comboBox1.Items.Add(reader.GetString(1).ToString());
                        }
                    }
                    conn.Close();
                }

            }
            int index = 0;
            if (ViewQ.inserimento_modo == 1)
            {
                index = 4;
            }
            else
            {
                index = comboBox1.FindString(ViewQ.vectS[pntSimb].classe);
                if(index == -1)
                {
                    string sc = ViewQ.vectS[pntSimb].classe;
                    if(sc.Contains("RETI")) index = 5;
                }
            }

            comboBox1.SelectedIndex = index;

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    cmd.CommandText = "select * from linea where linea.codice_classe = '" + comboBox1.Text + "'";
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            comboBox2.Items.Add(reader.GetString(0).ToString());
                            comboBox3.Items.Add(reader.GetString(2).ToString());
                        }
                    }
                }
                conn.Close();
            }

            if (ViewQ.inserimento_modo == 1)
            {
                index = 0;
            }
            else
            {
            index = comboBox2.FindString(ViewQ.vectS[pntSimb].codice);
            }

            comboBox2.SelectedIndex = index;
            comboBox3.SelectedIndex = index;

        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = 0;
            for (i = 0; i < ViewQ.nSimb;++i )
            {
                string codex = ViewQ.vectS[i].codice;
                if (codex.CompareTo(comboBox2.Text) == 0) break;
            }

            if (i == ViewQ.nSimb)
            {
                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from linea where codice_stclasse = '" + comboBox2.Text + "'";
                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {

                            while (reader.Read())
                            {
                                ViewQ.vectS[i].codice = reader.GetString(0).ToString();

                                ViewQ.vectS[i].denom = reader.GetString(2).ToString();

                                ViewQ.vectS[i].penType = int.Parse(reader.GetString(3).ToString());
                                ViewQ.vectS[i].tratto = reader.GetString(4).ToString();
                                ViewQ.vectS[i].visib = 1;
                                string s1 = reader.GetString(5).ToString();


                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectS[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                                ViewQ.vectS[i].selected = 1;
                            }
                        }
                        conn.Close();
                    }
                    ViewQ.vectS[i].classe = comboBox1.Text;
                    ViewQ.vectS[i].elemento = ViewQ.vectS[i-1].elemento;        //  trovare una migliore definizione
                }
                ++ViewQ.nSimb;
            }

            int isel = 0;
            if (ViewQ.inserimento_modo == 0)
            {
                isel = ViewQ.selected_linea;
                int pntSimb = ViewQ.vectShapes[isel].pntSimb;
                if (i != pntSimb && ViewQ.vectShapes[isel].modifica == 0) ViewQ.vectShapes[isel].modifica = 1;
                ViewQ.vectShapes[isel].pntSimb = i;

                if (textBox6.Text.Contains("1")) textBox6.Text = "2";
                ViewQ.vectShapes[isel].edizione = textBox6.Text;
                if(ViewQ.vectShapes[isel].modifica == 0) ViewQ.vectShapes[isel].modifica = 1;        //  da verificare
            }

            if (ViewQ.inserimento_modo == 1)
            {
                
                isel = ViewQ.nShapes;

                ViewQ.vectShapes[isel].pntSimb = i;
                int numID = ViewQ.vectShapes[isel - 1].numID;
                ViewQ.vectShapes[isel].numID = numID + 1;

                int nVertices = ViewQ.vectShapes[isel - 1].nVertices;
                int newPnt = ViewQ.vectShapes[isel - 1].pntpL + nVertices;
     //           ViewQ.vectShapes[isel].pntpL = newPnt;
                ViewQ.vectShapes[isel].nVertices = ViewQ.newVertices;

                nVertices = ViewQ.newVertices;
                int pntl = newPnt;
                int Xmin = ViewQ.PL[pntl].X;
                int Xmax = ViewQ.PL[pntl].X;
                int Ymin = ViewQ.PL[pntl].Y;
                int Ymax = ViewQ.PL[pntl].Y;
                for (int j = 1; j < nVertices; ++j)
                {
                    if (Xmin > ViewQ.PL[pntl].X) Xmin = ViewQ.PL[pntl].X;
                    if (Xmax < ViewQ.PL[pntl].X) Xmax = ViewQ.PL[pntl].X;
                    if (Ymin > ViewQ.PL[pntl].Y) Ymin = ViewQ.PL[pntl].Y;
                    if (Ymax < ViewQ.PL[pntl].Y) Ymax = ViewQ.PL[pntl].Y;
                    ++pntl;
                }
                ViewQ.vectShapes[isel].PointA.X = Xmin;
                ViewQ.vectShapes[isel].PointA.Y = Ymin;
                ViewQ.vectShapes[isel].PointB.X = Xmax;
                ViewQ.vectShapes[isel].PointB.Y = Ymax;
                ViewQ.vectShapes[isel].edizione = textBox6.Text;

                ViewQ.vectShapes[isel].modifica = 3;
                ++ViewQ.nShapes; 
                ViewQ.newPnt = 0;
            }
            if (ViewQ.inserimento_modo == 2)
            {
                isel = ViewQ.selected_linea;
                ViewQ.vectShapes[isel].pntSimb = i;
                {
                    int nVertices1 = ViewQ.vectShapes[isel].nVertices;
                    int nVertices = ViewQ.vectShapes[isel].nVertices;
 
                    int pntl = ViewQ.vectShapes[isel].pntpL;

                    int Xmin = ViewQ.PL[pntl].X;
                    int Xmax = ViewQ.PL[pntl].X;
                    int Ymin = ViewQ.PL[pntl].Y;
                    int Ymax = ViewQ.PL[pntl].Y;
                    for (int j = 1; j < nVertices; ++j)
                    {
                        if (Xmin > ViewQ.PL[pntl].X) Xmin = ViewQ.PL[pntl].X;
                        if (Xmax < ViewQ.PL[pntl].X) Xmax = ViewQ.PL[pntl].X;
                        if (Ymin > ViewQ.PL[pntl].Y) Ymin = ViewQ.PL[pntl].Y;
                        if (Ymax < ViewQ.PL[pntl].Y) Ymax = ViewQ.PL[pntl].Y;
                        ++pntl;
                    }
                    ViewQ.vectShapes[isel].PointA.X = Xmin;
                    ViewQ.vectShapes[isel].PointA.Y = Ymin;
                    ViewQ.vectShapes[isel].PointB.X = Xmax;
                    ViewQ.vectShapes[isel].PointB.Y = Ymax;
                    ViewQ.newPnt = 0;
                }
                if(ViewQ.vectShapes[isel].modifica == 0) ViewQ.vectShapes[isel].modifica = 1;
            }

            ViewQ.vectShapes[isel].quota = textBox4.Text;
            ViewQ.vectShapes[isel].lunghezza = textBox5.Text;

            Save.modificaLinea = 1; 
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
                string caption = "cancellazione linea";
                int isel = ViewQ.selected_linea;
                var result = MessageBox.Show("confermare la cancellazione della linea " + isel.ToString(), caption,
                              MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    if (ViewQ.vectShapes[isel].modifica == 3) ViewQ.vectShapes[isel].modifica = 5;
                    else
                    {
                        ViewQ.vectShapes[isel].modifica = 2;
                    }

                    Save.modificaLinea = 1;
                    this.DialogResult = DialogResult.OK;
                }
            this.Close();
        }


         private void comboBox1_Click(object sender, EventArgs e)
         {

         }

         private void comboBox2_Click(object sender, EventArgs e)
         {
             
             int index = comboBox2.SelectedIndex;
             if(index > -1)comboBox3.SelectedIndex = index;
             
         }

         private void comboBox3_Click(object sender, EventArgs e)
         {
             int index = comboBox3.SelectedIndex;
             if (index > -1)
             {
                 comboBox2.SelectedIndex = index;
                 comboBox2.Text = comboBox2.Items.IndexOf(index).ToString();
             }

         }

         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
         {
             int index = comboBox1.SelectedIndex + 1;

             using (SQLiteConnection conn = SetConnection())
             {
                 conn.Open();
                 using (SQLiteCommand cmd = new SQLiteCommand(conn))
                 {

                     cmd.CommandText = "select * from linea where linea.codice_classe = '"; 
                     cmd.CommandText = string.Concat(cmd.CommandText, index.ToString(),"'");
                     SQLiteDataReader reader = cmd.ExecuteReader();
                     if (reader.HasRows)
                     {
                         string comp = index.ToString();
                         comboBox2.Items.Clear();
                         comboBox3.Items.Clear();
                         while (reader.Read())
                         {
                             comboBox2.Items.Add(reader.GetString(0).ToString());
                             comboBox3.Items.Add(reader.GetString(2).ToString());
                         }
                     }
                 }
                 conn.Close();
             }
             index = 1;
             comboBox2.SelectedIndex = index;
             comboBox3.SelectedIndex = index;
         }

         private void comboBox3_SelectedValueChanged(object sender, EventArgs e)
         {
             int index = comboBox3.SelectedIndex;
             if (index > -1)
             {
                 comboBox2.SelectedIndex = index;
             }
         }

         private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
         {
             int index = comboBox2.SelectedIndex;
             if (index > -1) comboBox3.SelectedIndex = index;
         }

         private void button4_Click(object sender, EventArgs e)
         {
             int isel = 0;
             int Vertices = 0;
             if (ViewQ.inserimento_modo == 1)
             {
                 isel = ViewQ.nShapes;
                 Vertices = ViewQ.newVertices;
             }
             else
             {
                isel = ViewQ.selected_linea;
                Vertices = ViewQ.vectShapes[isel].nVertices;
                if (ViewQ.inserimento_modo == 2) Vertices += ViewQ.newVertices;
             }
              vectShapesQuota(isel);
              textBox4.Text = ViewQ.vectShapes[isel].quota;

        }

         private void button5_Click(object sender, EventArgs e)
         {
             int isel = 0;
             int Vertices = 0;
             if (ViewQ.inserimento_modo == 1)
             {
                 isel = ViewQ.nShapes;
                 Vertices = ViewQ.newVertices;
             }
             else
             {
                 isel = ViewQ.selected_linea;
                 Vertices = ViewQ.vectShapes[isel].nVertices;
                 if (ViewQ.inserimento_modo == 2) Vertices += ViewQ.newVertices;
             }

              vectShapesLunghezza(isel, Vertices);
              textBox5.Text = ViewQ.vectShapes[isel].lunghezza;
         }

         private void vectShapesQuota(int ipt)
         {

            MessageBox.Show("completare vectShapesQuota");
             return;
         }

         private void prova(Point Hit, int ival)
         {

             int idiff = (int)(ival / ViewQ.scalaX);
             int X1 = Hit.X - idiff;
             int Y1 = Hit.Y - idiff;
             int X2 = Hit.X + idiff;
             int Y2 = Hit.Y + idiff;

             ViewQ.dc0.RotateTransform(ViewQ.alpha);
             Point P1 = new Point();
             Point P2 = new Point();

             P1.X = (int)((X1 - ViewQ.Xmin) * ViewQ.scalaX) + ViewQ.Xoff;//
             P1.Y = (int)((Hit.Y - ViewQ.Ymin) * ViewQ.scalaY) + ViewQ.Yoff;//Y1

             P2.X = (int)((X2 - ViewQ.Xmin) * ViewQ.scalaX) + ViewQ.Xoff;//
             P2.Y = (int)((Hit.Y - ViewQ.Ymin) * ViewQ.scalaY) + ViewQ.Yoff;//Y1

             int dxx = P2.X - P1.X;

             Pen iPen = new Pen(Color.Red, 1);
  //           ViewQ.dc0.DrawLine(iPen, P1, P2);

             P1.X = (int)((Hit.X - ViewQ.Xmin) * ViewQ.scalaX) + ViewQ.Xoff;//
             P1.Y = (int)((Y1 - ViewQ.Ymin) * ViewQ.scalaY) + ViewQ.Yoff;//Y1

             P2.X = (int)((Hit.X - ViewQ.Xmin) * ViewQ.scalaX) + ViewQ.Xoff;//
             P2.Y = (int)((Y2 - ViewQ.Ymin) * ViewQ.scalaY) + ViewQ.Yoff;//Y1

             P1.X = (int)((X1 - ViewQ.Xmin) * ViewQ.scalaX) + ViewQ.Xoff;//
             P1.Y = (int)((Y2 - ViewQ.Ymin) * ViewQ.scalaY) + ViewQ.Yoff;//Y1

             Rectangle pntR = new Rectangle(P1.X,P1.Y,dxx,dxx);
             Pen iPen1 = new Pen(Color.Green, 1);
             ViewQ.dc0.DrawRectangle(iPen1, pntR);

             ViewQ.dc0.ResetTransform();

         }

         private void prova1(int ipt, int ipn, int ival)
         {
             int nVertices = ViewQ.vectShapes[ipt].nVertices;
             int pntpl = ViewQ.vectShapes[ipt].pntpL + ipn;

             double K1 = 0;
             double K2 = 0;

             if (ipn == 0)
             {
                 K2 = (ViewQ.PL[pntpl + 1].Y - ViewQ.PL[pntpl].Y) / (ViewQ.PL[pntpl + 1].X - ViewQ.PL[pntpl].X);
                 K1 = K2;

             }
             if (ipn == nVertices-1)
             {
                 K1 = (ViewQ.PL[pntpl].Y - ViewQ.PL[pntpl-1].Y) / (ViewQ.PL[pntpl].X - ViewQ.PL[pntpl-1].X);
                 K2 = K1;

             }
             else if(ipn > 0)
             {
                 K1 = (ViewQ.PL[pntpl].Y - ViewQ.PL[pntpl - 1].Y) / (ViewQ.PL[pntpl].X - ViewQ.PL[pntpl - 1].X);
                 K2 = (ViewQ.PL[pntpl + 1].Y - ViewQ.PL[pntpl].Y) / (ViewQ.PL[pntpl + 1].X - ViewQ.PL[pntpl].X);
             }

             double TH1, TH1_2;

             TH1 = (K2 - K1) /(1.0 + K1 * K2);
             TH1_2 = (-1.0 + Math.Sqrt(1.0 + TH1 * TH1)) / TH1;

             double TGB = (TH1_2 + K1)/(1 -TH1_2*K1);
             double COSB = 1.0/Math.Sqrt(1.0 + TGB * TGB);
             double SINB = COSB*TGB;

             int idiff = (int)(ival / ViewQ.scalaX);

             int dxx = (int)(COSB * idiff);
             int dyy = (int)(SINB*idiff);
             Point P1 = ViewQ.PL[pntpl];
             Point P2 = new Point(P1.X + dxx, P1.Y + idiff);
             Point P3 = new Point(P1.X - dxx, P1.Y - idiff); 
             Pen iPen = new Pen(Color.Red, 1);
             ViewQ.dc0.RotateTransform(ViewQ.alpha);

             ViewQ.dc0.DrawLine(iPen, P1, P2);
             ViewQ.dc0.DrawLine(iPen, P1, P3);

             ViewQ.dc0.ResetTransform();

         }

        private void vectShapesLunghezza(int ipt, int nVertices)
         {
            MessageBox.Show("necessita introdurre nei calcoli l'altezza del nodo");
             double dlen = 0;
             int pntl = ViewQ.vectShapes[ipt].pntpL;
             int pntl1 = pntl + 1;
             for (int j = 1; j < nVertices; ++j)
             {
                 double dx = ViewQ.PL[pntl1].X - ViewQ.PL[pntl].X;
                 double dy = ViewQ.PL[pntl1].Y - ViewQ.PL[pntl].Y;
                 double dd = Math.Sqrt(dx * dx + dy * dy);
                 dlen += dd;
                 ++pntl;
                 ++pntl1;
             }
             ViewQ.vectShapes[ipt].lunghezza = dlen.ToString("F");

         }


         private void drawLinea1(int ipt, int opt)
         {

             NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

             int nVertices = ViewQ.vectShapes[ipt].nVertices;
             int ipnt = ViewQ.vectShapes[ipt].pntpL;
             int pntSimb = ViewQ.vectShapes[ipt].pntSimb;

             if (ViewQ.vectS[pntSimb].selected == 0) goto L40;
             int visib = ViewQ.vectS[pntSimb].visib;
             if (visib == 0) goto L40;

             ViewQ.dc0.RotateTransform(ViewQ.alpha);

             Color icolor = new Color();
             if (opt == 1) icolor = ViewQ.vectS[pntSimb].icolor;
             else icolor = Color.Red;
             string ss = ViewQ.vectS[pntSimb].tratto;
             int type = ViewQ.vectS[pntSimb].penType;
             float width = 0F;
  //           float interpasso = 0F;
    //         float diam = 0F;
             string s1;
             float[] dashValues = { 0, 0 };

   //          Scarpata scr = new Scarpata();

             switch (type)
             {
                 case 1:
                     int ipos = ss.IndexOf('/');
                     s1 = ss.Substring(0, ipos);
                     width = float.Parse(s1, nfi);
                     break;

                 case 40:         //  curva di livello intermedia
                 case 41:         //  curva di livello direttrice
                     ipos = ss.IndexOf('/');
                     s1 = ss.Substring(0, ipos);
                     width = float.Parse(s1, nfi);
                     break;

                 default:
                     width = 0F;
                     break;
             }
             if (width == 0F) goto L40;

             Pen iPen = new Pen(icolor, width);
             if (type != 12 && dashValues[0] > 0F) iPen.DashPattern = dashValues;

             Point[] polygon = new Point[nVertices];
             for (int j = 0; j < nVertices; ++j)
             {
                 polygon[j].X = (int)((ViewQ.PL[ipnt].X - ViewQ.Xmin) * ViewQ.scalaX) + ViewQ.Xoff;
                 polygon[j].Y = (int)((ViewQ.PL[ipnt].Y - ViewQ.Ymin) * ViewQ.scalaY) + ViewQ.Yoff;
                 ++ipnt;
                 Rectangle pntR = new Rectangle(polygon[j].X, polygon[j].Y, 4, 4);
                 if (type != 12) ViewQ.dc0.DrawEllipse(iPen, pntR);
             }

             ViewQ.dc0.DrawCurve(iPen, polygon);

         L40: ;
             ViewQ.dc0.ResetTransform();

         }
    }
}
